import { loginAuthorizations } from '@/api/login';
import { clearToken, setToken } from '@/utils';
// import axios from 'axios';
import * as types from '../actionTypes/loginTypes';
import * as typesUser from '../actionTypes/userTypes';

export const login = loginData => {
  return async dispatch => {
    const res = await loginAuthorizations(loginData);
    // const res = await axios.post(
    //   'http://geek.itheima.net/v1_0/authorizations',
    //   loginData
    // );
    const {
      data: { token },
      message,
    } = res.data;
    if (message === 'OK') {
      // 登陆成功，存储token
      setToken(token);
      dispatch({ type: types.LOGIN_SETTOKEN, payload: token });
    }
  };
};

export const logout = () => {
  return dispatch => {
    // 清除token
    dispatch({ type: types.LOGOUT_CLEANTOKEN });
    clearToken();

    // 清除用户信息
    dispatch({ type: typesUser.USER_CLEANINFO });
  };
};
